package com.wdz.utils.tenxunyuncos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.Bucket;
import com.qcloud.cos.region.Region;

import java.util.List;

/**
 * 腾讯云对象存储客户端操作工具类
 */
public class COSClientUtil {

    /**
     * 获取腾讯云cos客户端对象
     * @return
     */
    public static COSClient getCOSClient(String regionName){
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "AKIDBBvRhhe46KLE3pEFumswxxunZEpDqnty";
        String secretKey = "8YoGct2pkRs65HDuGQM993pR1ampnbEi";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }

    public static void main(String[] args) {

        COSClient cosClient = getCOSClient("ap-shanghai");
        List<Bucket> buckets = cosClient.listBuckets();
        for (Bucket bucketElement : buckets) {
            String bucketName = bucketElement.getName();
            String bucketLocation = bucketElement.getLocation();
            System.out.println(bucketName+":"+bucketLocation);
        }
    }

}
